package com.matchmealpha;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ApplyFilterActivity extends Activity implements OnClickListener {

	ImageView imageView;
	Gallery gallery;
	Bitmap oImage;
	Bitmap curBit;
	Button backButton;
	Button doneButton;
	RelativeLayout loadingPanel;
	String filename;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.apply_filter_view);

		Bundle bund = this.getIntent().getExtras();

	//	filename = bund.getString(CameraActivity.IMAGE_FILE_NAME);

		// Toast.makeText(this, filename, Toast.LENGTH_SHORT).show();
		Bitmap image = null;
	
		image = ShareActivity.ORIGINAL_IMAGE;

		oImage = image;
		curBit = Bitmap.createBitmap(oImage);
		imageView = (ImageView) findViewById(R.id.FilterImageView);
		loadingPanel = (RelativeLayout) findViewById(R.id.loadingPanel);
		backButton = (Button) findViewById(R.id.ApplyFilterLeftButton);
		doneButton = (Button) findViewById(R.id.ApplyFilterRightButton);

		imageView.setImageBitmap(image);
		loadingPanel.setVisibility(View.GONE);

		gallery = (Gallery) findViewById(R.id.FilterThumbnailGallery);
		gallery.setAdapter(new ImageAdapter(this));

		backButton.setOnClickListener(this);
		doneButton.setOnClickListener(this);

		gallery.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {

				if (v.getId() == R.drawable.original_thumbnail) {

					Toast.makeText(ApplyFilterActivity.this, "Original",
							Toast.LENGTH_SHORT).show();
					imageView.setImageBitmap(oImage);
				} else if (v.getId() == R.drawable.sepia_thumbnail) {
					Toast.makeText(ApplyFilterActivity.this, "Sepia Filter",
							Toast.LENGTH_SHORT).show();

					// Later.. stop the animation
					// imageView.setAnimation(null);
					imageView.setVisibility(View.GONE);
					loadingPanel.setVisibility(View.VISIBLE);
					new ApplyFilterTask().execute(new Integer(
							FilterEffects.COOL_SEPIA), oImage);
				}else if(v.getId() == R.drawable.original_rounded_corners_thumbnail) {
					imageView.setVisibility(View.GONE);
					loadingPanel.setVisibility(View.VISIBLE);
					
					new ApplyFilterTask().execute(new Integer(
							FilterEffects.ROUNDED_EDGES), oImage);
					
				}
			}
		});
	}

	private class ApplyFilterTask extends AsyncTask<Object, Void, Bitmap> {
		protected Bitmap doInBackground(Object... filter) {
			return FilterEffects.applyFilter((Bitmap) filter[1],
					((Integer) filter[0]).intValue());
		}

		protected void onPostExecute(Bitmap result) {
			imageView.setVisibility(View.VISIBLE);
			loadingPanel.setVisibility(View.GONE);
			curBit = result;
			imageView.setImageBitmap(result);
		}
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		Intent in;
		//int[] pixels = new int[300 * 300];
		switch (arg0.getId()) {
		case R.id.ApplyFilterLeftButton:

			this.setResult(RESULT_CANCELED);
			this.finish();
			break;

		case R.id.ApplyFilterRightButton:

			//curBit.getPixels(pixels, 0, 300, 0, 0, 300, 300);
			in = new Intent();
			ShareActivity.CURRENT_IMAGE = curBit;
			this.setResult(RESULT_OK, in);
			this.finish();

			break;
		}

	}

}